#include "KNormalFileReader.h"

using namespace KCS0017;

long long KNormalFileReader::Read(void* pvBuffer, size_t nLength)
{
	assert(NULL != pvBuffer);
	if (INVALID_HANDLE_VALUE == m_hFile)
	{
		return ~0LL;
	}
	if (0 == nLength)
	{
		return 0;
	}

	DWORD dwBytesRead;
	if(0 == ReadFile(m_hFile, pvBuffer, nLength, &dwBytesRead, NULL))
	{
		return ~0LL;
	}
	return (long long)dwBytesRead;
}

int KNormalFileReader::Seek(long long lPosition)
{
	LONG lLow32 = lPosition & 0xFFFFFFFF; // 32-bit mask.
	LONG lHigh32 = (lPosition >> 32) & 0xFFFFFFFF;
	if (INVALID_SET_FILE_POINTER ==
		SetFilePointer(m_hFile, lLow32, &lHigh32, FILE_BEGIN))
	{
		return 1;
	}
	return 0;
}

int KNormalFileReader::SetFile(WCHAR const* szFileName)
{
	m_hFile = CreateFile(
		szFileName,
		GENERIC_READ,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL
	);
	return INVALID_HANDLE_VALUE == m_hFile;
}

KNormalFileReader::KNormalFileReader():
	m_hFile(INVALID_HANDLE_VALUE)
{
	// pass
}

KNormalFileReader::~KNormalFileReader()
{
	// pass
}
